/**
 * 公告资料维护
 * @author yaohuan
 * @date 2018/12/19
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        layedit,
        table,
        table_notice = {
            GWDH002: ''
        },
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            , {
                field: 'GWDH002', width: 160, title: '公告类型', fixed: 'left', sort: true,
                filterDatas: app.datas_('sys_notice_gwdh002'),
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {
                field: 'GWDH005',
                width: 80,
                title: '公告日期',
                sort: true,
                search: 'default',
                searchOpts: {type: 'daterange'}
            }
            , {field: 'GWDH004_NAME', width: 80, title: '发布人员', sort: true, search: 'default', searchOpts: {icon: true}}
            , {field: 'GWDH006', width: 80, title: '失效日期', search: 'default', searchOpts: {type: 'daterange'}}
            , {field: 'GWDH001', width: 137, title: '发布主旨', search: 'default'}
            , {
                field: 'GWDHACTI',
                width: 137,
                title: '有效码',
                search: 'default',
                filterDatas: app.datas_('sys_y_n').concat([{code: '', value: '全部'}]),
                searchOpts: {type: 'select', default: ''}
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-noticesetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        noticeSettingAction
    ;

    var Param = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'notice',
                _searchs: [{
                    id: 'notice', tableId: 'notice_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'notice_edit', tpl: 'system/notice_edit'
                },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'notice_table'),
                    url: g_ctxPath + 'sys/notice/getList',
                    // exportsUrl: g_ctxPath + 'sys/notice/exportsParam',
                    // exportsName: '公告资料数据',
                    title: '公告资料信息表',
                    method: 'post',
                    id: 'notice_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_notice,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-noticesetting-action').innerHTML).render({}, function (string) {
                    noticeSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(notice_table)', function (obj) {

                    layer.tips(noticeSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'notice_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts: {
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.del(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(notice_table)', function (obj) {
                    self._openTplFull({
                        tag: 'notice_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '公告资料信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(notice_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'notice_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'notice_table', where: table_notice});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'sys/notice/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '公告资料删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('notice_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('gwdh');
                    _temp.GWDHENT = v.GWDHENT;
                    _temp.GWDHDOCNO = v.GWDHDOCNO;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'sys/notice/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '公告资料删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'notice',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWDH004_NAME') {//发布人员开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config:{
                                            method:'q_sysuser_id',
                                            columnList:[
                                                {label: '交易对象编号', prop: 'ID', width: '150'},
                                                {label: '交易对象名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDH004 = data.ID;
                                            vue.isearch.fields.GWDH004_NAME = data.NAME;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择发布人员',
                                        area: ['600px', '430px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.GWDH004_NAME': function (val) {
                            if (val == '' || val == null) {
                                this.isearch.fields.GWDH004 = '';
                            }
                        },
                        'isearch.fields.GWDH005': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWDH005_START = newValue[0];
                                this.isearch.fields.GWDH005_END = newValue[1];
                            } else {
                                this.isearch.fields.GWDH005_START = '';
                                this.isearch.fields.GWDH005_END = '';
                            }
                        },
                        'isearch.fields.GWDH006': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWDH006_START = newValue[0];
                                this.isearch.fields.GWDH006_END = newValue[1];
                            } else {
                                this.isearch.fields.GWDH006_START = '';
                                this.isearch.fields.GWDH006_END = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
            layui.use(['layedit'], function () {
                layedit = layui.layedit;
            });
        }
    });

    return new Param();

});
